import { getToken } from '@/utils/storage'
export function getConn (url) {
  var token = getToken()
  console.log('ws token', token)
  return initWebSocket(url, [token])
}

function initWebSocket (url, protocol) { // 初始化weosocket
  const conn = new WebSocket(url, protocol)
  conn.onopen = open.bind(conn)
  conn.onerror = error.bind(conn)
  conn.onclose = close.bind(conn)
  return conn
}

function open () { // 连接建立之后执行send方法发送数据
  // const actions = { test: '12345' }
  // this.send(JSON.stringify(actions))
}

function error (e) { // 连接建立失败重连
  console.log('error', e, this)
  // this.initWebSocket()
}

function close (e) { // 关闭
  console.log('断开连接', e)
  this.close()
}

class Test {
  constructor () {
    console.log('hello')
  }
}
export default new Test()
